<?php
/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version
*/

namespace Poodle\Auth;

class Admin
{
	public
		$allowed_methods = array('GET','HEAD','POST');

	public function GET()
	{
		$K = \Poodle::getKernel();

		$providers = array();
		$auth_providers = $K->SQL->query("SELECT
			auth_provider_id id,
			auth_provider_class class,
			auth_provider_mode mode,
			auth_provider_name name
		FROM {$K->SQL->TBL->auth_providers}");
		foreach ($auth_providers as $auth)
		{
			$auth['uid']    = preg_replace('#[^a-z0-9]#','-',mb_strtolower($auth['name']));
			$auth['detect'] = array();
			$detections = $K->SQL->query("SELECT
				auth_detect_name name,
				auth_detect_regex regex,
				auth_detect_discover_uri discover_uri
			FROM {$K->SQL->TBL->auth_providers_detect}
			WHERE auth_provider_id={$auth['id']}");
			foreach ($detections as $detect)
			{
				$auth['detect'][] = $detect;
			}
			$providers[] = $auth;
		}

		$K->OUT->auth_providers = $providers;
		$K->OUT->display('poodle/auth/admin');
	}

	public function POST()
	{
		$tbl = \Poodle::getKernel()->SQL->TBL->auth_providers;
		foreach ($_POST['auth_provider'] as $id => $data)
		{
			if (!empty($data['class']) && class_exists($data['class'])) {
				$tbl->update(array(
					'auth_provider_class' => $data['class'],
					'auth_provider_mode'  => empty($data['mode']) ? 0 : array_sum($data['mode']),
					'auth_provider_name'  => $data['name']
				), 'auth_provider_id='.$id);
			}
		}
		\Poodle\URI::redirect($_SERVER['REQUEST_URI']);
	}

}
